package cn.com.backstage.controller;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiT_city;
import cn.com.api.entity.ApiType;

import cn.com.api.service.ApiT_cityService;
import cn.com.api.service.StartOnLoadService;

import cn.com.util.*;
import cn.com.util.*;

@Controller
@RequestMapping(Utils.SYS_BACKSTAGE + "/")
public class CityController {

	private static Logger logger = Logger.getLogger(CityController.class);

	@Autowired
	private ApiT_cityService apiT_cityService;

	/**
	 * 进入查询界面
	 * 
	 * @return
	 */
	@RequestMapping(value = "city-list")
	public ModelAndView list(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/city_list.jsp");

		List<ApiT_city> citys = apiT_cityService.findT_citysByIsOpen(1);
		mv.addObject("citys", citys);

		return mv;
	}

	/**
	 * 进入新增或者编辑页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "city-add-pre")
	public ModelAndView city_add_pre(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/city_add.jsp");
		//
		if (request.getParameter("id") != null) {
			String id = request.getParameter("id");
			ApiT_city city = apiT_cityService.findT_cityById(Integer.valueOf(id));
			mv.addObject("city", city);
		}
		return mv;
	}

	/**
	 * 新增或者编辑
	 * 
	 * @return
	 */
	@RequestMapping(value = "city-add")
	public void city_add(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String name = request.getParameter("name");
		String u_name = request.getParameter("u_name");
		String latitude = request.getParameter("latitude");
		String longitude = request.getParameter("longitude");
		String is_open = request.getParameter("is_open");
		String send_distance = request.getParameter("send_distance");
		String is_auto_allocateorder = request.getParameter("is_auto_allocateorder");
		String same_distance = request.getParameter("same_distance");

		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");
		// add
		if (id == null || (id.trim()).equals("")) {
			ApiT_city t_city = new ApiT_city();
			t_city.setName(name);
			t_city.setUname(u_name);
			t_city.setIs_open(1);
			t_city.setLatitude(Float.valueOf(latitude));
			t_city.setLongitude(Float.valueOf(longitude));
			t_city.setIs_open(Integer.valueOf(is_open));
			t_city.setSend_distance(Float.valueOf(send_distance));
			t_city.setIs_auto_allocateorder(Integer.valueOf(is_auto_allocateorder));
			t_city.setSame_distance(Double.valueOf(same_distance));
			
			apiT_cityService.insertT_city(t_city);
			map.put(Utils.MESSAGE, "添加成功！");
		}
		// edit
		else {
			ApiT_city t_city = apiT_cityService.findT_cityById(Integer.valueOf(id));
			t_city.setName(name);
			t_city.setUname(u_name);
			t_city.setLatitude(Float.valueOf(latitude));
			t_city.setLongitude(Float.valueOf(longitude));
			t_city.setIs_open(Integer.valueOf(is_open));
			t_city.setSend_distance(Float.valueOf(send_distance));
			t_city.setIs_auto_allocateorder(Integer.valueOf(is_auto_allocateorder));
			t_city.setSame_distance(Double.valueOf(same_distance));

			apiT_cityService.updateT_city(t_city);
			map.put(Utils.MESSAGE, "编辑成功！");
		}
		//
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	@RequestMapping(value = "city-delete")
	public void city_delete(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");

		// delete
		int ret_i = apiT_cityService.deleteT_city(Integer.valueOf(id));

		//
		if (ret_i > 0) {
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

}
